package com.enzoism.springboot.dive.bis.clone;

import java.util.Date;

public class JavaCloneTest {
    /**
     * @param args
     */
    public static void main(String[] args) throws CloneNotSupportedException {
        Date date = new Date();
        JavaClone javaClone = new JavaClone("xiaoming", Integer.valueOf(10), 20, date);
        System.out.println("-->对象：" + javaClone + " name：" + javaClone.getUserName() + " age：" + javaClone.getAge() + " count：" + javaClone.getCountNum() + " date：" + javaClone.getDate());

        // 拷贝的对象是同一份
        JavaClone javaCopy = javaClone;
        System.out.println("-->对象：" + javaCopy + " name：" + javaCopy.getUserName() + " age：" + javaCopy.getAge() + " count：" + javaCopy.getCountNum() + " date：" + javaCopy.getDate());


        // 浅拷贝
        JavaClone clone = (JavaClone)javaClone.clone();
        // 深拷贝
        JavaClone deepClone = javaClone.myClone();


        javaClone.setAge(100);
        javaClone.setCountNum(Integer.valueOf(20));
        date.setDate(date.getDate()-1);
        javaClone.setDate(date);
        javaClone.setUserName("enzoism");

        System.out.println("-->【原】对象：" + javaClone + " name：" + javaClone.getUserName() + " age：" + javaClone.getAge() + " count：" + javaClone.getCountNum() + " date：" + javaClone.getDate());
        System.out.println("-->【浅】对象：" + clone + " name：" + clone.getUserName() + " age：" + clone.getAge() + " count：" + clone.getCountNum() + " date：" + clone.getDate());
        System.out.println("-->【深】对象：" + clone + " name：" + deepClone.getUserName() + " age：" + deepClone.getAge() + " count：" + deepClone.getCountNum() + " date：" + deepClone.getDate());


    }
}
